﻿using System;
using System.Collections.Generic;
using System.Text;
using Common;
using Dto;
using SqlServerDAL;

namespace Business
{
    public class AdminBusiness
    {
        private const string Waming = "Business.AdminBusiness";
        private readonly AdminDAL adminDal = new AdminDAL();
        private readonly AdminRoleDAL adminRoleDal = new AdminRoleDAL();
        private StringBuilder _sbChildMenu = new StringBuilder();

        public AdminDto AdminLogin(string account, string pwd)
        {
            try
            {
                AdminDto admindto = null;
                var ht = adminDal.GetAdmin(account, pwd);
                if (ht != null)
                {
                    admindto = FillDto.FillAdmindto(ht);
                    admindto.RoleDto = GetAdminRole(admindto.AdminRoleID);
                    //admindto.RoleDto.RoleContent = ",WZGLY,WZGL,HYGL,DDGL,CPGL,YXGL,DJGL,ZXGL,SJGL,WZGLY-WZGLY,WZGLY-XJGLY,WZGLY-XJGLY-GLYLB,CPGL-CPGL,CPGL-CPGL-XZCP,SJGL-HTSJGL,SJGL-HTSJGL-CDGL,CSYX,CPGL-CPGL-CPLB,CPGL-CPGL-Liebiao,HYGL-WZHYGL,HYGL-WZHYGL-TJWZHY,HYGL-WZHYGL-HYLB,DDGL-DDGL,DDGL-DDGL-DDLB,WZGL-WZGL,WZGL-WZGL-MSTD,WZGL-WZGL-GYWM,WZGL-WZGL-LXFS,WZGL-WZGL-DLJM,YXGL-YXGL,YXGL-YXGL-GGGL,DJGL-DJGL,DJGL-DJGL-SCEWM,'";
                }
                return admindto;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".AdminLogin(string account, string pwd)", ex);
                return null;
            }
        }

        public AdminDto GetAdminById(int id)
        {
            try
            {
                AdminDto dto = null;
                var ht = adminDal.FindById(id);
                if (ht != null)
                {
                    dto = FillDto.FillAdmindto(ht);
                    dto.RoleDto = GetAdminRole(dto.AdminRoleID);
                }
                return dto;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".GetAdminById(int id)", ex);
                return null;
            }
        }

        public List<AdminDto> GetAdminList(int pageNo, int pageSize, out int totalCount)
        {
            try
            {
                List<AdminDto> dtos = null;
                var hts = adminDal.SlicedFindAll((pageNo - 1) * pageSize, pageSize);
                if (hts != null && hts.Count > 0)
                {
                    dtos = new List<AdminDto>();
                    foreach (var ht in hts)
                    {
                        AdminDto dto = FillDto.FillAdmindto(ht);
                        dto.RoleDto = GetAdminRole(dto.AdminRoleID);
                        dtos.Add(dto);
                    }
                }
                totalCount = adminDal.SelectCount("");
                return dtos;
            }
            catch (Exception ex)
            {
                totalCount = -1;
                Log.LogHandler.LogError(Waming + ".GetAdminList(int pageNo, int pageSize)", ex);
                return null;
            }
        }

        public List<AdminRoleDto> GetAllRoles()
        {
            //List<AdminRoleDto> dtos = null;
            //var hts = adminRoleDal.FindAll();
            //if (hts != null && hts.Count > 0)
            //{
            //    dtos = new List<AdminRoleDto>();
            //    foreach (var ht in hts)
            //    {
            //        var dto = FillDto.FillAdminRoledto(ht);
            //        adminRoleDal.
            //        var countQuery = new CountQuery(typeof(Admin), new[] { Restrictions.Eq("AdminRoleId", dto.RoleID) });
            //        dto.RoleAdminCount = Convert.ToInt32(ActiveRecordBase.ExecuteQuery(countQuery));
            //        dtos.Add(dto);
            //    }
            //}
            //return dtos;
            return null;
        }

        public bool SaveRole(AdminRoleDto dto)
        {
            try
            {
                //var entity = new AdminRole
                //                 {
                //                     RoleContent = dto.RoleContent,
                //                     RoleID = dto.RoleID,
                //                     RoleName = dto.RoleName,
                //                     RoleTime = dto.RoleTime
                //                 };
                //entity.Save();
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".SaveRole(AdminRoledto dto)", ex);
                return true;
            }
        }

        private string CreateChildMenu(ICollection<ManageMenuDto> childMenuList, ICollection<string> roleMenus,
                                       int superiorId, ICollection<string> userRoleMenus)
        {
            var mmb = new ManageMenuBusiness();
            if (childMenuList != null && childMenuList.Count > 0)
            {
                _sbChildMenu.Append("<tr id=\"superiorTr" + superiorId + "\"><td class=\"subTd\" colspan=\"2\"><table>");
                foreach (ManageMenuDto childMenu in childMenuList)
                {
                    IList<ManageMenuDto> menuList = mmb.GetMenuBySuperiorId(childMenu.MenuID, false, userRoleMenus);
                    _sbChildMenu.Append("<tr id=\"tr" + childMenu.MenuKey +
                                        "\"><td class=\"subTd\"><a href=\"javascript:FoldingMenu('" + childMenu.MenuID +
                                        "');\");\">");
                    _sbChildMenu.Append("<img id=\"img" + childMenu.MenuID +
                                        "\" src=\"/Content/images/CloseSub.gif\" /></a>");
                    if (roleMenus != null && roleMenus.Contains(childMenu.MenuKey))
                    {
                        _sbChildMenu.Append("<input type=\"checkBox\" name=\"RoleContents\" value=\"" +
                                            childMenu.MenuKey + "\" checked=\"checked\">" + childMenu.MenuName);
                    }
                    else
                    {
                        _sbChildMenu.Append("<input type=\"checkBox\" name=\"RoleContents\" value=\"" +
                                            childMenu.MenuKey + "\">" + childMenu.MenuName);
                    }
                    _sbChildMenu.Append("</tr>");
                    if (menuList != null && menuList.Count > 0)
                    {
                        CreateChildMenu(menuList, roleMenus, childMenu.MenuID, userRoleMenus);
                    }
                }
                _sbChildMenu.Append("</table></td></tr>");
            }
            return _sbChildMenu.ToString();
        }

        #region BuildRoleList[构建权限菜单列表]

        /// <summary>
        /// 构建权限菜单列表
        /// </summary>
        /// <param name="roleMenus"></param>
        /// <param name="userRoleMenus"></param>
        /// <returns></returns>
        public string BuildRoleList(List<string> roleMenus, ICollection<string> userRoleMenus)
        {
            var mmb = new ManageMenuBusiness();
            IList<ManageMenuDto> menuList = mmb.GetMenuBySuperiorId(0, true, userRoleMenus);
            var sb = new StringBuilder();
            foreach (ManageMenuDto firstMenu in menuList)
            {
                sb.Append("<tr id=\"tr" + firstMenu.MenuKey + "\">");
                sb.Append("<td><a id=\"aShow\" href=\"javascript:FoldingMenu('" + firstMenu.MenuID +
                          "');\"><img id=\"img" + firstMenu.MenuID + "\" src=\"/Content/images/closeSub.gif\"></a>");
                if (roleMenus != null && roleMenus.Contains(firstMenu.MenuKey))
                {
                    sb.Append("<input type=\"checkBox\" name=\"RoleContents\" value=\"" + firstMenu.MenuKey +
                              "\" checked=\"checked\">");
                }
                else
                {
                    sb.Append("<input type=\"checkBox\" name=\"RoleContents\" value=\"" + firstMenu.MenuKey + "\">");
                }
                sb.Append(firstMenu.MenuName + "</td></tr>");
                _sbChildMenu = new StringBuilder();
                sb.Append(CreateChildMenu(firstMenu.ChildMenuList, roleMenus, firstMenu.MenuID, userRoleMenus));
            }

            return sb.ToString();
        }

        #endregion

        #region GetAdminRole[通过ID获取角色内容]

        /// <summary>
        /// 通过ID获取角色内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AdminRoleDto GetAdminRole(int id)
        {
            try
            {
                AdminRoleDto dto = null;
                var entity = adminRoleDal.FindById(id);
                if (entity != null)
                {
                    dto = FillDto.FillAdminRoledto(entity);
                }
                return dto;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".GetAdminRole(int id)", ex);
                return null;
            }
        }

        #endregion
    }
}